package com.blog.cloud.chat.handler;

import cn.hutool.core.util.StrUtil;
import com.blog.cloud.chat.message.chat.SendResponse;
import com.blog.cloud.chat.message.chat.SendToSingleMessage;
import com.blog.cloud.chat.utils.WebSocketUtil;
import jakarta.websocket.Session;
import org.springframework.stereotype.Component;

@Component
public class SendToSingleHandler implements MessageHandler<SendToSingleMessage> {
    @Override
    public void execute(Session session, SendToSingleMessage message) {
        String userId = message.getUserId();
        if (StrUtil.isBlank(userId)) {
            SendResponse sendResponse = new SendResponse();
            sendResponse.setCode(400);
            sendResponse.setMsg("目标用户ID不能为空");
            WebSocketUtil.sendResponse(session, getType(), sendResponse);
        }

        WebSocketUtil.sendToSingle(userId, getType(), message);
    }

    @Override
    public String getType() {
        return SendToSingleMessage.TYPE;
    }
}
